package com.cskaoyan.shopping.controller;

import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.mall.constant.ShoppingRetCode;
import com.cskaoyan.mall.dto.ProductDetailRequest;
import com.cskaoyan.mall.dto.ProductDetailResponse;
import com.cskaoyan.shopping.dto.*;
import com.cskaoyan.shopping.form.PageInfo;
import com.cskaoyan.shopping.form.PageResponse;
import com.cskaoyan.shopping.service.IProductCateService;
import com.cskaoyan.shopping.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: 牟治宇
 * @description 查看商品详情
 * @date: 2022/7/29 15:37
 */

@RestController
@RequestMapping("/shopping")
public class ProdcuctController {

    @Autowired
    IProductService productService;

    /**
     * @author 牟治宇
     * @description 商品详情
     * @date 2022/7/29 20:36
     * @param [id]
     * @return com.cskaoyan.mall.commons.result.ResponseData
     */
    @GetMapping("/product/{id}")
    public ResponseData getProductDetail(@PathVariable("id") Long id) {
        ProductDetailRequest request = new ProductDetailRequest();
        request.setId(id);
        ProductDetailResponse response = productService.getProductDetail(request);
        if (response.getCode().equals(ShoppingRetCode.SUCCESS.getCode())) {
            return new ResponseUtil().setData(response.getProductDetailDto());
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }


    /**
     * @author 牟治宇
     * @description 分页查询商品
     * @date 2022/7/29 20:36
     * @param [pageInfo]
     * @return com.cskaoyan.mall.commons.result.ResponseData
     */
    @GetMapping("/goods")
    public ResponseData getProductListByPage(PageInfo pageInfo) {
        AllProductRequest request = new AllProductRequest();
        request.setPage(pageInfo.getPage());
        request.setSize(pageInfo.getSize());
        request.setSort(pageInfo.getSort());
        request.setPriceGt(pageInfo.getPriceGt());
        request.setPriceLte(pageInfo.getPriceLte());
        AllProductResponse response = productService.getAllProduct(request);
        if (response.getCode().equals(ShoppingRetCode.SUCCESS.getCode())) {
            PageResponse pageResponse = new PageResponse();
            pageResponse.setTotal(response.getTotal());
            pageResponse.setData(response.getProductDtoList());
            return new ResponseUtil().setData(pageResponse);

        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }

    /**
     * @author 牟治宇
     * @description 商品推荐
     * @date 2022/7/29 20:37
     * @param []
     * @return com.cskaoyan.mall.commons.result.ResponseData
     */
    @GetMapping("/recommend")
    public ResponseData searchRecommendList() {
        RecommendResponse response = productService.getRecommendGoods();

        if (response.getCode().equals(ShoppingRetCode.SUCCESS.getCode())) {
            return new ResponseUtil().setData(response.getPanelContentItemDtos());
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }


    /*
    * @author Invoker
    * @description {}
    * @date 2022/7/30 9:45
    * @param
    * @return 周子伟
    */
    @Autowired
    IProductCateService iProductCateService;
    @GetMapping("/categories")
public  ResponseData getCategories( String sort) {

        AllProductCateRequest request = new AllProductCateRequest();
        request.setSort(sort);
        AllProductCateResponse response = iProductCateService.getAllProductCate(request);
        if (response.getCode().equals(ShoppingRetCode.SUCCESS.getCode())) {
            return new ResponseUtil().setData(response.getProductCateDtoList());

        }

        return new ResponseUtil().setErrorMsg(response.getMsg());

    }





}
